<body>
<script type="text/javascript">  
Ext.onReady(function() {
<#assign grid=page.grid />
<#assign form=page.form />
<#assign maxFieldWidth=0/>
<#list form.field as item>
	<#if (item.width??&&item.width > maxFieldWidth) >
			<#assign maxFieldWidth=item.width/>
	</#if>
</#list>
<#assign windowWidth=(page.form.labelWidth+maxFieldWidth+8)*form.colnum+50 />
var destroyObjs=[];
var eidtform=null;
var eidtwindow=null;
function getEidtWindow(){
    if(eidtwindow===null){
		 eidtform=new Ext.form.FormPanel(<#include  "form.ftl" >);
		 destroyObjs[0]=eidtform;
		 eidtwindow=new Ext.Window({
								title : '',
								width : ${windowWidth},
								closeAction : 'hide',
								plain : true,
								actionUrl:"",
								labelAlign : "right",
								buttonAlign : "center",
								resizable:false,
								modal:true,
								padding:"2px 2px",
								items : [eidtform],
								buttons : [ {text:"保存",
											   handler : function(){
											   		submitFormPanel(eidtform, eidtwindow.actionUrl, function(form,action){
											   			
											   			if(eidtwindow.isadd===true){
											   				Ext.MessageBox.confirm('提示', action.result.info+",是否清空表单", function(btn) {
																if (btn == 'yes') {
																	eidtform.getForm().reset();
																}
															});
											   			}else{
											   				Ext.Msg.alert("提示",action.result.info);
											   			}
											   				store.reload();
											   		});
											   
											   }
											},
											{text:"关闭",
											   handler : function() {
													eidtwindow.hide();	
											 	}
											}
										  ]
					
						});
		destroyObjs[1]=eidtwindow;
		return eidtwindow;
	}else{
		return eidtwindow;
	}
}
	var sm = new Ext.grid.CheckboxSelectionModel({
				singleSelect : false
			});
	var columnarray=${grid.colModelJSON()};
	columnarray.unshift(sm);
	var cm = new Ext.grid.ColumnModel(columnarray);
	var dataRecord = Ext.data.Record.create(${grid.dataRecordJSON()});
    var frm1 =null; 
    var vwindow =null;
	var store = new Ext.data.Store({
			 autoLoad : {
					params : {
						start : 0,
						limit : 30
					}
				},
				proxy : new Ext.data.HttpProxy({
						url : basepath+'../sysControl/dic/DicManagerController/queryDic.do?tablename=${page.tablename}'

						}),

				reader : new Ext.data.JsonReader({
							totalProperty : 'results',
							root : "items",
							id : 'logid'
						}, dataRecord)
			});

var pagingBar = new Ext.PagingToolbar({// 分页工具栏
		store : store,
		pageSize : 30,// 每页显示的条数。用于系统自动计算它的start值
		displayInfo : true,
		displayMsg : '数据从第{0}条 - 第{1} 条 共 {2}条数据',
		emptyMsg : "没有记录"

	});

	var grid = new Ext.ux.grid.LockingGridPanel({
		cm : cm,
		sm : sm,
		width:frame_center.getInnerWidth()-4,
		height:frame_center.getInnerHeight()-28,
		store : store,
		renderTo:'dicManager_table_${page.tablename}',
		tbar : [{
			text : '添加',
			iconCls : 'table_add',
			handler : function() {
			var window=getEidtWindow();
			window.setTitle('添加');
			window.actionUrl= basepath+'../sysControl/dic/DicManagerController/addDic.do?tablename=${page.tablename}';
			window.isadd=true;
			eidtform.getForm().reset();
			window.show();
			}
		},{
			text : '修改',
			iconCls : 'table_edit',
			handler : function() {
			var record = grid.getSelectionModel().getSelections();
				if (record.length <= 0) {
						Ext.MessageBox.alert('提示', '请选择需要修改的记录');
						return;
					} else if (record.length > 1) {
						Ext.MessageBox.alert('提示', '最多只能选择一行');
						return;				
					}
			var window=getEidtWindow();
			window.isadd=false;
			window.setTitle('修改');
			window.actionUrl= basepath+'../sysControl/dic/DicManagerController/updateDic.do?tablename=${page.tablename}';
			if(!window.rendered)
			window.show();
			eidtform.getForm().load({
				 method:'POST',
				 url : basepath+'../sysControl/dic/DicManagerController/formDataLoad.do?tablename=${page.tablename}',
				 params: {  
                       ${keycolumn.name}:record[0].data.${keycolumn.name} 
                  },
				 success : function(form, action) {
					if(window.rendered===true&&window.hidden===true )
						window.show();
   				},
  				 failure : function(form, action) {
   				 	Ext.Msg.alert('数据加载失败');
   				 	window.hide();
   				}

  			});
			
			}
		},{
			text : '删除',
			iconCls : 'table_delete',
			handler : function() {
				var record = grid.getSelectionModel().getSelections();
				if (record.length <= 0) {
						Ext.MessageBox.alert('提示', '请选择需要删除的记录');
						return;
				} 
				var url= basepath+'../sysControl/dic/DicManagerController/deleteDic.do?tablename=${page.tablename}';
				var ids=[];
				for(var i=0;i<record.length;i++){
					ids.push(record[i].data.${keycolumn.name});
				}
				
				 deleteData(url, "ids="+ids.join(","), store, "确定要删除选择记录吗?");
			}
		},'->',{text : '导出所有数据',iconCls:'page_excel',handler:function(){var url= basepath+'../sysControl/dic/DicManagerController/outExcel.do?name=${page.tablename}';var oW = window.open(url, "openExcel${page.tablename}" );}},
			{xtype:'outputExcelButton',title:'${page.tablename}数据'},{xtype:'closePage'}
		],
		bbar : pagingBar
	});
	<#if (filterfields?size>0) >
		<#list filterfields as item>
			var ${item.name}_filter=<@typeMacro[item.type] field=item/>;
			${item.name}_filter.editable=true;
			${item.name}_filter.allowBlank=true;
		</#list>
		
		var tbar2 = new Ext.Toolbar({
					renderTo : grid.tbar,
					items : [
							<#list filterfields as item>
							  '${item.fieldLabel}',${item.name}_filter,
							</#list>
							{ text:'查询',iconCls:'table_find',tooltip : '查询',
							  	handler : function() {
							  		var filterfields=tbar2.items;
							  		var n=filterfields.length-1;
							  		console.log(filterfields);
							  		for(var i=1;i<n;i+=2){
							  		 	var field=filterfields.get(i)
								 	 	addStoreBaseParams(field,field.hiddenName?field.hiddenName:field.name,store);
									}
									 store.load({params:{start:0,limit:30}});
								}
							}
					]
				});
			destroyObjs[2]=tbar2;
	</#if>
	
	
	destroyResize(grid,destroyObjs);
	
	

});
</script>
 <div id="dicManager_table_${page.tablename}" style="heigth:100xp"></div>
</body>
